package com.iris.dataStructures;

import java.util.Arrays;

/**
 *
 */
public class QuickSort4 {
    public static void main(String args[]) {
        QuickSort4 quicksort = new QuickSort4();
        int[] arrays = new int[]
                {10, 12, 5, 13, 3, 14, 4, 15, 2, 16, 17, 17, 177, 18, 8, 8, 19};
//                {10, 12, 5};
        System.out.println(Arrays.toString(arrays));
        quicksort.quickSort(arrays,0,arrays.length-1);

        System.out.println(Arrays.toString(arrays));
    }

    public  void quickSort(int[] numbers, int start, int end) {
        if (start < end) {
            // 选定的基准值（第一个数值作为基准值）
            int base = numbers[start];
            int temp; // 记录临时中间值
            int i = start, j = end;
            do {
                while ((numbers[i] < base) && (i < end))
                    i++;
                while ((numbers[j] > base) && (j > start))
                    j--;
                if (i <= j) {
                    temp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = temp;
                    i++;
                    j--;
                }
                System.out.println("==="+Arrays.toString(numbers));
            } while (i <= j);
            if (start < j)
                quickSort(numbers, start, j);
            if (end > i)
                quickSort(numbers, i, end);
        }
    }
}
